'use strict'

import  BasicAttri from './basicAttri'
import FightAttri from './fightAttri'
import BasicInfo from './basicInfo'
import Task from './task'
import PackageComponent from './package'
import {totalConfig} from '../config/templateConfig'

let playerConfig = totalConfig.player[100000]
let basicAttri = new BasicAttri(playerConfig.basicAttri)
let localStorage = window.localStorage
//角色基础信息
class Player {
    constructor(opts) {
        this.name = opts.name
        this.basicInfo = new BasicInfo(opts.basicInfo) //基础信息
        this.basicAttri = new BasicAttri(opts.basicAttri) //基础属性
        this.fightAttri = new FightAttri() //战斗属性
        this.task = new Task(opts.task) //任务
        this.bag = new PackageComponent(opts.bag) //背包
        this.initPlayer()
    }

    initPlayer() { //初始化角色信息
        this.fightAttri = new FightAttri({})
        this.calcBasicAttri()
        this.fightAttri.hp = this.fightAttri.maxHp
    }

    calcBasicAttri() { //计算基础属性
        let fightAttri = this.fightAttri
        fightAttri.attack += basicAttri.attack //攻击力
        fightAttri.maxHp += basicAttri.maxHp //生命
        fightAttri.maxMp += basicAttri.maxMp //魔法
        fightAttri.defend += basicAttri.defend //防御
        fightAttri.name = this.name
    }

    getFightAttriCopy() { //获取战斗属性副本
        let fightAttri = new FightAttri(this.fightAttri)
        fightAttri.hp = this.fightAttri.hp
        return fightAttri
    }

    save() { //保存角色信息
        let content = JSON.stringify(this)
       localStorage.setItem(this.name, content)
    }

}
module.exports = Player
